use super::{ice::IceMessage, rrai::RraiMessage, webrtc::WebRTCMessage};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(tag = "type")]
pub enum SignalMessageContent {
    #[serde(rename = "ping")]
    Ping,
    #[serde(rename = "pong")]
    Pong,
    #[serde(rename = "webrtc")]
    WebRTC {
        err: Option<String>,
        data: Option<WebRTCMessage>,
    },
    #[serde(rename = "ice")]
    Ice {
        err: Option<String>,
        data: Option<IceMessage>,
    },
    #[serde(rename = "rrai")]
    Rrai {
        err: Option<String>,
        data: Option<RraiMessage>,
    },
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct SignalMessage {
    #[serde(rename = "src")]
    pub src: String,
    #[serde(rename = "data")]
    pub data: Option<SignalMessageContent>,
    #[serde(rename = "err")]
    pub err: Option<String>,
    #[serde(rename = "request_id")]
    pub request_id: Option<u64>,
}

impl SignalMessage {}

impl From<&String> for SignalMessage {
    fn from(msg: &String) -> Self {
        let msg_str = msg.as_str();
        tracing::debug!("{}", msg_str);
        serde_json::from_str(msg_str).expect("signal message unmarshal")
    }
}

impl From<&SignalMessage> for String {
    fn from(msg: &SignalMessage) -> Self {
        serde_json::to_string(msg).expect("signal message marshal")
    }
}
